package com.parking.status.sync.netty.client;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Administrator on 2015/9/15.
 */
@Component
public class ClientChannelInitializer extends ChannelInitializer<SocketChannel> {


    @Autowired
    ResponseFinalDecoder responseFinalDecoder;

    @Autowired
    ResponseBodyDecoder responseBodyDecoder;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();

//        pipeline.addLast("prepender", new WholeFrameEncoder());
//
//        //检查空闲断开连接
////        pipeline.addLast("idleStateCheck", new IdleStateHandler(0, 0, 600));
////        pipeline.addLast("idleCheckHandler", new IdleStateCheckHandler());
//
//        pipeline.addLast("bodymessage", new BodyFrameEncoder());
//        pipeline.addLast("content", new ContentEncoder());


        pipeline.addLast("DealHead", new ResponseHeadDecoder());
        pipeline.addLast("BufToString", new ResponseUrlStringDecoder());
        pipeline.addLast("JasonToResponse", responseBodyDecoder);
        pipeline.addLast("ResponseFinal", responseFinalDecoder);

        pipeline.addLast("RequestFinal", new RequestFinalEncoder());
        pipeline.addLast("StringToBuf", new RequestUrlStringEncoder());
        pipeline.addLast("RequestToJason", new RequestBodyEncoder());
        pipeline.addLast("Initial", new RequestInitialEncoder());
    }
}